#ifndef HELMETINPUTS_H
#define HELMETINPUTS_H

#include "HelmetSystemConfig.h"

/**************************************************************************************
	Global Variables
**************************************************************************************/
unsigned int LogHangTime = 0; // Not logging hang-time
unsigned short int PrevButtonRed = 0; // button Red unpressed previously
unsigned short int PrevButtonGrn = 0;
unsigned short int PrevButtonBlu = 0;

/**************************************************************************************
	Definitions
**************************************************************************************/
// Accelerometer zero-g detect
// Port B bit 15 is also used by ADC channel 15
#define Read0GD() mPORTBReadBits( BIT_15 ) // read the 0-g detect from accelerometer
#define Config0GD() mPORTBSetPinsDigitalIn( BIT_15 )

// use pins 76-84 for button inputs
#define PORTD_PB_MASK	( BIT_1 | BIT_2 | BIT_3 | BIT_12 | BIT_13 | BIT_4 | BIT_5 | BIT_6 | BIT_7 )
#define ConfigButtons()	mPORTDSetPinsDigitalIn( PORTD_PB_MASK )
#define BUTTON1			BIT_1
#define BUTTON2			BIT_2
#define BUTTON3			BIT_3
#define BUTTON4			BIT_12
#define BUTTON5			BIT_13
#define BUTTON6			BIT_4
#define BUTTON7			BIT_5
#define BUTTON8			BIT_6
#define BUTTON9			BIT_7
#define ButtonIsPressed()	( mPORTDRead() & 0x30FE )
#define Button1Pressed()	( mPORTDReadBits( BUTTON1 ) )
#define Button2Pressed()	( mPORTDReadBits( BUTTON2 ) )
#define Button3Pressed()	( mPORTDReadBits( BUTTON3 ) )
#define Button4Pressed()	( mPORTDReadBits( BUTTON4 ) )
#define Button5Pressed()	( mPORTDReadBits( BUTTON5 ) )
#define Button6Pressed()	( mPORTDReadBits( BUTTON6 ) )
#define Button7Pressed()	( mPORTDReadBits( BUTTON7 ) )
#define Button8Pressed()	( mPORTDReadBits( BUTTON8 ) )
#define Button9Pressed()	( mPORTDReadBits( BUTTON9 ) )

#define ButtonRedPressed()	Button7Pressed()
#define ButtonGrnPressed()	Button8Pressed()
#define ButtonBluPressed()	Button9Pressed()

/**************************************************************************************
	Function Prototypes
**************************************************************************************/
void ConfigInputs( void );

/**************************************************************************************
	Functions
**************************************************************************************/
void ConfigInputs() {
	// Config accelerometer zero-g detect
	Config0GD();
	// Config buttons
	ConfigButtons();
}	

void CheckPushbuttons() {
	// If in free fall, increment hang time counter
    if( Read0GD() ) {
	    LogHangTime++;
	}
	// If not in free fall and hang time has been counted,
	// tell main to report the hang time
	else if( LogHangTime > 0 ) {
		DoInMain( ReportHangTime ); // report hang time
	}
	
	if( ButtonIsPressed() ) {
		if( ButtonRedPressed() ) {
			if( PrevButtonRed == 0 ) {
				DoInMain( BlankHudButton );	
			}
			PrevButtonRed = 1;
		} else {
			PrevButtonRed = 0;
		}
		
		if( ButtonGrnPressed() ) {
			if( PrevButtonGrn == 0 ) {
				DoInMain( ChangeModeButton );
			}
			PrevButtonGrn = 1;	
		} else {
			PrevButtonGrn = 0;
		}
		
		if( ButtonBluPressed() ) {
			if( PrevButtonBlu == 0 ) {
				DoInMain( LogDataButton );	
			}
			PrevButtonBlu = 1;	
		} else {
			PrevButtonBlu = 0;
		}			
	}	
}

float GetHangTime() {
	float actht = ( LogHangTime*1.0 - 1 ) / T1_FREQ ; // hangtime = logged hangtime / frequency of T1 interrupt
	LogHangTime=0;
	return actht;
}

#endif